/**
 * User: Obi
 * Date: 02.08.11
 * Time: 20:09
 */
package com.jediflasher.game_client.data.world {
	import com.jediflasher.game_client.data.BaseData;
	import com.jediflasher.game_client.data.UniverseData;
	import com.jediflasher.game_client.events.model.CharacterDataEvent;
	import com.jediflasher.game_client.server.messages.CharacterInfoResponse;
	import com.netease.protobuf.Message;

	[Event(name="nameChange", type="com.jediflasher.game_client.events.model.CharacterDataEvent")]
	[Event(name="positionChange", type="com.jediflasher.game_client.events.model.CharacterDataEvent")]
	public class CharacterData extends BaseData {

		public function CharacterData(universe:UniverseData, id:int) {
			super(universe);
			this.id = id;
		}

		public var id:int;

		public var name:String;

		public var x:Number;

		public var y:Number;

		override public function read(message:Message):void {
			var characterMessage:CharacterInfoResponse = message as CharacterInfoResponse;

			if (characterMessage.hasName && this.name != characterMessage.name) {
				this.name = characterMessage.name;
				super.dispatchEvent(new CharacterDataEvent(CharacterDataEvent.NAME_CHANGE));
			}

			var positionChanged:Boolean = false;
			if (characterMessage.hasXPos && this.x != characterMessage.xPos) {
				this.x = characterMessage.xPos;
				positionChanged = true;
			}

			if (characterMessage.hasYPos && this.y != characterMessage.yPos) {
				this.y = characterMessage.yPos;
				positionChanged = true;
			}

			if (positionChanged) super.dispatchEvent(new CharacterDataEvent(CharacterDataEvent.POSITION_CHANGE));
		}
	}
}
